package com.company.zonghelizi.jihe.data;

import java.util.*;

/**
 * 利用集合完成象数据库那样存储数据，并且可以简单查询，利用map存储学生信息，字段如下： id ,name,age，实现步骤：
 */
class Database{
    private Map<String,Students> map = new HashMap<>();
    public Collection<Students> select(){
        return map.values();
    }

    public Students select(Integer id){
        return map.get(id);
    }
    public void insert(Students student){
        map.put(student.getId(),student);
    }
    public boolean update(Students student){
        if(map.containsKey(student.getId())){
            map.put(student.getId(),student);
            return true;
        }
        return false;
    }
    public boolean delete(Integer id){
        if(map.containsKey(id)){
            map.remove(id);
            return true;
        }
        return false;
    }
}
class TestSan{
    public static void main(String[] args) {
        boolean blag = true;
        Scanner sc = new Scanner(System.in);
        Database d = new Database();
        while (blag){
            System.out.println("请输入一个数字");
            System.out.println("1. 向表中插入值\n2. 查询表中的值\n3. 修改表中的值\n4. 删除表中的值\n其他退出");
            switch (sc.nextInt()){
                case 1:
                    d.insert(input());
                    System.out.println("成功插入一条记录");
                    break;
                case 2:
                    System.out.println("请选择查询的方式：1. 查询单行 2. 查询全表");
                    switch (sc.nextInt()){
                        case 1:
                            System.out.println("请输入查询Id");
                            System.out.println("学号\t姓名\t年龄");
                            System.out.println(d.select(sc.nextInt()));
                            break;
                        case 2:
                            Collection<Students> c = d.select();
                            System.out.println("学号\t姓名\t年龄");
                            for (Students s:c) {
                                System.out.println("学号"+s.getId()+"\t姓名"+s.getName()+"\t年龄"+s.getAge());
                            }
                            break;
                        default:
                            System.out.println("输入不正确");
                    }
                    break;
                case 3:
                    if(d.update(input())){
                        System.out.println("成功修改一条记录");
                    }else {
                        System.out.println("没有该id这条记录");
                    }
                    break;
                case 4:
                    System.out.println("请输入删除学生的ID");
                   if(d.delete(sc.nextInt())){
                       System.out.println("成功删除一条记录");
                   }else {
                       System.out.println("未能删除，没有该id这条记录");
                   }
                   break;
                default:
                    blag = false;
                    System.exit(0);  // 强制退出虚拟机
            }
        }
    }
    // 用户输入学生信息
    public static Students input(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生ID");
        String id = sc.next();
        System.out.println("请输入学生姓名");
        String name = sc.next();
        int age = 0;
        while (true){
            System.out.println("请输入学生的年龄");
            String strAge = sc.next();
            if(strAge.matches("\\d{1,2}")){
                age = sc.nextInt();
                break;
            }
        }
        Students stu = new Students(id,name,age);
        return stu;
    }
}

class Students{
    private String name;
    private int age;
    private String id;

    public Students(String id,String name, int age) {
        this.name = name;
        this.age = age;
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


    
}

